// import { get, post, ssePost } from './base'
// import type { IOnCompleted, IOnData, IOnError, IOnFile, IOnMessageEnd, IOnMessageReplace, IOnThought } from './base'
import type { ChatPromptConfig, CompletionPromptConfig } from '@/models/debug'
import type { ModelModeType } from '@/services/types/app'
import type { ModelParameterRule } from '@/components/header/account-setting/model-provider-page/declarations'

type Fetcher<T, M> = any;
const ssePost = <T>(args: any, args2: any): any => { 
  
}
const post = <T>(args: any, args2: any): any => { }
const get = <T>(args: any, args2?: any, args3?: any): any => { }
const patch = <T>(args: any, args2?: any): any => { }
const del = <T>(args: any, args2?: any): any => { }
const put = <T>(args: any, args2?: any): any => { }

type IOnCompleted = any
type IOnData = any
type IOnError = any
type IOnFile = any
type IOnMessageEnd = any
type IOnMessageReplace = any
type IOnThought = any

export type AutomaticRes = {
  prompt: string
  variables: string[]
  opening_statement: string
  error?: string
}
export type CodeGenRes = {
  code: string
  language: string[]
  error?: string
}

export const sendChatMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onThought, onFile, onError, getAbortController, onMessageEnd, onMessageReplace }: {
  onData: IOnData
  onCompleted: IOnCompleted
  onFile: IOnFile
  onThought: IOnThought
  onMessageEnd: IOnMessageEnd
  onMessageReplace: IOnMessageReplace
  onError: IOnError
  getAbortController?: (abortController: AbortController) => void
}) => {

  console.log('sendChatMessage', appId, body);

  return ssePost(`apps/${appId}/chat-messages`, {
    body: {
      ...body,
      response_mode: 'streaming',
    },
  }, { onData, onCompleted, onThought, onFile, onError, getAbortController, onMessageEnd, onMessageReplace })
}

export const stopChatMessageResponding = async (appId: string, taskId: string) => {
  return post(`apps/${appId}/chat-messages/${taskId}/stop`)
}

export const sendCompletionMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onError, onMessageReplace }: {
  onData: IOnData
  onCompleted: IOnCompleted
  onError: IOnError
  onMessageReplace: IOnMessageReplace
}) => {
  return ssePost(`apps/${appId}/completion-messages`, {
    body: {
      ...body,
      response_mode: 'streaming',
    },
  }, { onData, onCompleted, onError, onMessageReplace })
}

export const fetchSuggestedQuestions = (appId: string, messageId: string, getAbortController?: any) => {
  return get(
    `apps/${appId}/chat-messages/${messageId}/suggested-questions`,
    {},
    {
      getAbortController,
    },
  )
}

export const fetchConversationMessages = (appId: string, conversation_id: string, getAbortController?: any) => {
  // appId, conversation_id
  console.log(appId, conversation_id, getAbortController);

  return {
    "limit": 20,
    "has_more": false,
    "data": [
      {
        "id": "9aec3e1b-2b06-488e-960f-905431bf380f",
        "conversation_id": "658cd5f1-19a8-4f39-9855-7fbf19d94f2d",
        "inputs": {
          "a": "\u9ec4\u7199\u54f2"
        },
        "query": "2222",
        "message": [
          {
            "role": "system",
            "text": "``` xml\n<instruction>\n\u8bf7\u6839\u636e\u4ee5\u4e0b\u6b65\u9aa4\u5b8c\u6210\u4efb\u52a1\uff1a\n1. \u56f4\u7ed5\u6982\u8981\u7684\u8f6f\u4ef6\u9700\u6c42\u8fdb\u884c\u7ec6\u5316\u76f8\u5173\u5185\u5bb9\uff0c\u4fdd\u8bc1\u903b\u8f91\u5b8c\u6574\u3002\n2. \u6839\u636e\u7ec6\u5316\u540e\u7684\u8f6f\u4ef6\u9700\u6c42\u4ece\u4e2d\u63d0\u53d6\u6240\u6709\u6d89\u53ca\u7684\u5bf9\u8c61\u3002\n3. \u5bf9\u6bcf\u4e2a\u5bf9\u8c61\u8fdb\u884c\u5206\u6790\uff0c\u5217\u51fa\u5176\u6240\u6709\u5c5e\u6027\u3002\n4. \u5bf9\u6bcf\u4e2a\u5bf9\u8c61\u8fdb\u884c\u5206\u6790\uff0c\u5217\u51fa\u5bf9\u8c61\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\n5. \u786e\u4fdd\u8f93\u51fa\u4e0d\u5305\u542b\u4efb\u4f55XML\u6807\u7b7e\u3002\n</instruction>\n\n<example>\n\u8f6f\u4ef6\u9700\u6c42\u63cf\u8ff0\uff1a\u7528\u6237\u53ef\u4ee5\u6ce8\u518c\u8d26\u6237\uff0c\u767b\u5f55\u540e\u67e5\u770b\u4e2a\u4eba\u4fe1\u606f\u548c\u8ba2\u5355\u5386\u53f2\u3002\n\u5206\u6790\u7ed3\u679c\uff1a\n\u5bf9\u8c61\uff1a\u7528\u6237\n\u5c5e\u6027\uff1a\u7528\u6237ID\u3001\u7528\u6237\u540d\u79f0\u3001\u7528\u6237\u5bc6\u7801\n\u5173\u7cfb\uff1a\u7528\u6237\u4fe1\u606f\u4e00\u5bf9\u4e00\u5173\u7cfb\u3001\u4e0e\u767b\u5f55\u65e5\u5fd7\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u4e0e\u8ba2\u5355\u4e00\u5bf9\u591a\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u7528\u6237\u4fe1\u606f\n\u5c5e\u6027\uff1a\u7528\u6237ID\u3001\u624b\u673a\u53f7\u3001\u90ae\u7bb1\u3001\u5730\u5740\n\u5173\u7cfb\uff1a\u7528\u6237\u4fe1\u606f\u4e00\u5bf9\u4e00\u5173\u7cfb\u3001\u4e0e\u767b\u5f55\u65e5\u5fd7\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u4e0e\u8ba2\u5355\u4e00\u5bf9\u591a\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u767b\u5f55\u65e5\u5fd7\n\u5c5e\u6027\uff1a\u7528\u6237ID\u3001\u767b\u5f55\u65f6\u95f4\u3001\u767b\u5f55IP\n\n\u5bf9\u8c61\uff1a\u5546\u54c1\n\u5c5e\u6027\uff1a\u5546\u54c1ID\u3001\u5546\u54c1\u540d\u79f0\u3001\u91d1\u989d\u3001\u5546\u54c1\u4fe1\u606f\n\n\u5bf9\u8c61\uff1a\u8ba2\u5355\n\u5c5e\u6027\uff1a\u8ba2\u5355ID\u3001\u7528\u6237ID\u3001\u8ba2\u5355SN\u3001\u5546\u54c1ID\u3001\u91d1\u989d\u3001\u652f\u4ed8\u72b6\u6001\u3001\u5730\u5740\u4fe1\u606f\n\n......\n</example>\n```\n",
            "files": []
          },
          {
            "role": "user",
            "text": "2222",
            "files": []
          }
        ],
        "message_tokens": 411,
        "answer": "\u8f6f\u4ef6\u9700\u6c42\u63cf\u8ff0\uff1a\u7528\u6237\u53ef\u4ee5\u6ce8\u518c\u8d26\u6237\uff0c\u767b\u5f55\u540e\u67e5\u770b\u4e2a\u4eba\u4fe1\u606f\u3001\u8ba2\u5355\u5386\u53f2\uff0c\u5e76\u80fd\u8fdb\u884c\u5546\u54c1\u641c\u7d22\u548c\u8d2d\u4e70\u3002\n\n\u5206\u6790\u7ed3\u679c\uff1a\n\u5bf9\u8c61\uff1a\u7528\u6237\n\u5c5e\u6027\uff1a\u7528\u6237ID\u3001\u7528\u6237\u540d\u79f0\u3001\u7528\u6237\u5bc6\u7801\u3001\u6ce8\u518c\u65f6\u95f4\u3001\u8d26\u6237\u72b6\u6001\n\u5173\u7cfb\uff1a\u7528\u6237\u4fe1\u606f\u4e00\u5bf9\u4e00\u5173\u7cfb\u3001\u4e0e\u767b\u5f55\u65e5\u5fd7\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u4e0e\u8ba2\u5355\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u4e0e\u8d2d\u7269\u8f66\u4e00\u5bf9\u4e00\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u7528\u6237\u4fe1\u606f\n\u5c5e\u6027\uff1a\u7528\u6237ID\u3001\u624b\u673a\u53f7\u3001\u90ae\u7bb1\u3001\u5730\u5740\u3001\u5934\u50cf\n\u5173\u7cfb\uff1a\u7528\u6237\u4fe1\u606f\u4e00\u5bf9\u4e00\u5173\u7cfb\u3001\u4e0e\u767b\u5f55\u65e5\u5fd7\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u4e0e\u8ba2\u5355\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u4e0e\u8d2d\u7269\u8f66\u4e00\u5bf9\u4e00\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u767b\u5f55\u65e5\u5fd7\n\u5c5e\u6027\uff1a\u7528\u6237ID\u3001\u767b\u5f55\u65f6\u95f4\u3001\u767b\u5f55IP\u3001\u8bbe\u5907\u4fe1\u606f\n\u5173\u7cfb\uff1a\u767b\u5f55\u65e5\u5fd7\u4e0e\u7528\u6237\u4e00\u5bf9\u591a\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u5546\u54c1\n\u5c5e\u6027\uff1a\u5546\u54c1ID\u3001\u5546\u54c1\u540d\u79f0\u3001\u4ef7\u683c\u3001\u5e93\u5b58\u3001\u63cf\u8ff0\u3001\u5206\u7c7b\n\u5173\u7cfb\uff1a\u5546\u54c1\u4e0e\u8ba2\u5355\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u5546\u54c1\u4e0e\u8d2d\u7269\u8f66\u4e00\u5bf9\u591a\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u8ba2\u5355\n\u5c5e\u6027\uff1a\u8ba2\u5355ID\u3001\u7528\u6237ID\u3001\u8ba2\u5355SN\u3001\u5546\u54c1ID\u3001\u91d1\u989d\u3001\u652f\u4ed8\u72b6\u6001\u3001\u4e0b\u5355\u65f6\u95f4\u3001\u5730\u5740\u4fe1\u606f\n\u5173\u7cfb\uff1a\u8ba2\u5355\u4e0e\u7528\u6237\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u8ba2\u5355\u4e0e\u5546\u54c1\u4e00\u5bf9\u591a\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u8d2d\u7269\u8f66\n\u5c5e\u6027\uff1a\u7528\u6237ID\u3001\u5546\u54c1ID\u3001\u6570\u91cf\u3001\u603b\u91d1\u989d\n\u5173\u7cfb\uff1a\u8d2d\u7269\u8f66\u4e0e\u7528\u6237\u4e00\u5bf9\u4e00\u5173\u7cfb\u3001\u8d2d\u7269\u8f66\u4e0e\u5546\u54c1\u591a\u5bf9\u591a\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u652f\u4ed8\n\u5c5e\u6027\uff1a\u652f\u4ed8ID\u3001\u8ba2\u5355ID\u3001\u652f\u4ed8\u65b9\u5f0f\u3001\u652f\u4ed8\u72b6\u6001\u3001\u652f\u4ed8\u65f6\u95f4\n\u5173\u7cfb\uff1a\u652f\u4ed8\u4e0e\u8ba2\u5355\u4e00\u5bf9\u4e00\u5173\u7cfb \n\n\u5bf9\u8c61\uff1a\u5546\u54c1\u641c\u7d22\n\u5c5e\u6027\uff1a\u641c\u7d22ID\u3001\u7528\u6237ID\u3001\u641c\u7d22\u5173\u952e\u8bcd\u3001\u641c\u7d22\u65f6\u95f4\n\u5173\u7cfb\uff1a\u5546\u54c1\u641c\u7d22\u4e0e\u7528\u6237\u4e00\u5bf9\u591a\u5173\u7cfb",
        "answer_tokens": 347,
        "provider_response_latency": 26.549835792000067,
        "from_source": "console",
        "from_end_user_id": null,
        "from_account_id": "2099825c-856e-4ce8-b035-05df64e53470",
        "feedbacks": [],
        "workflow_run_id": null,
        "annotation": null,
        "annotation_hit_history": null,
        "created_at": 1737937172,
        "agent_thoughts": [
          {
            "id": "b13bea8a-375c-41b3-b896-72ee0bc2cdc0",
            "chain_id": null,
            "message_id": "9aec3e1b-2b06-488e-960f-905431bf380f",
            "position": 1,
            "thought": "\u8f6f\u4ef6\u9700\u6c42\u63cf\u8ff0\uff1a\u7528\u6237\u53ef\u4ee5\u6ce8\u518c\u8d26\u6237\uff0c\u767b\u5f55\u540e\u67e5\u770b\u4e2a\u4eba\u4fe1\u606f\u3001\u8ba2\u5355\u5386\u53f2\uff0c\u5e76\u80fd\u8fdb\u884c\u5546\u54c1\u641c\u7d22\u548c\u8d2d\u4e70\u3002\n\n\u5206\u6790\u7ed3\u679c\uff1a\n\u5bf9\u8c61\uff1a\u7528\u6237\n\u5c5e\u6027\uff1a\u7528\u6237ID\u3001\u7528\u6237\u540d\u79f0\u3001\u7528\u6237\u5bc6\u7801\u3001\u6ce8\u518c\u65f6\u95f4\u3001\u8d26\u6237\u72b6\u6001\n\u5173\u7cfb\uff1a\u7528\u6237\u4fe1\u606f\u4e00\u5bf9\u4e00\u5173\u7cfb\u3001\u4e0e\u767b\u5f55\u65e5\u5fd7\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u4e0e\u8ba2\u5355\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u4e0e\u8d2d\u7269\u8f66\u4e00\u5bf9\u4e00\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u7528\u6237\u4fe1\u606f\n\u5c5e\u6027\uff1a\u7528\u6237ID\u3001\u624b\u673a\u53f7\u3001\u90ae\u7bb1\u3001\u5730\u5740\u3001\u5934\u50cf\n\u5173\u7cfb\uff1a\u7528\u6237\u4fe1\u606f\u4e00\u5bf9\u4e00\u5173\u7cfb\u3001\u4e0e\u767b\u5f55\u65e5\u5fd7\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u4e0e\u8ba2\u5355\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u4e0e\u8d2d\u7269\u8f66\u4e00\u5bf9\u4e00\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u767b\u5f55\u65e5\u5fd7\n\u5c5e\u6027\uff1a\u7528\u6237ID\u3001\u767b\u5f55\u65f6\u95f4\u3001\u767b\u5f55IP\u3001\u8bbe\u5907\u4fe1\u606f\n\u5173\u7cfb\uff1a\u767b\u5f55\u65e5\u5fd7\u4e0e\u7528\u6237\u4e00\u5bf9\u591a\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u5546\u54c1\n\u5c5e\u6027\uff1a\u5546\u54c1ID\u3001\u5546\u54c1\u540d\u79f0\u3001\u4ef7\u683c\u3001\u5e93\u5b58\u3001\u63cf\u8ff0\u3001\u5206\u7c7b\n\u5173\u7cfb\uff1a\u5546\u54c1\u4e0e\u8ba2\u5355\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u5546\u54c1\u4e0e\u8d2d\u7269\u8f66\u4e00\u5bf9\u591a\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u8ba2\u5355\n\u5c5e\u6027\uff1a\u8ba2\u5355ID\u3001\u7528\u6237ID\u3001\u8ba2\u5355SN\u3001\u5546\u54c1ID\u3001\u91d1\u989d\u3001\u652f\u4ed8\u72b6\u6001\u3001\u4e0b\u5355\u65f6\u95f4\u3001\u5730\u5740\u4fe1\u606f\n\u5173\u7cfb\uff1a\u8ba2\u5355\u4e0e\u7528\u6237\u4e00\u5bf9\u591a\u5173\u7cfb\u3001\u8ba2\u5355\u4e0e\u5546\u54c1\u4e00\u5bf9\u591a\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u8d2d\u7269\u8f66\n\u5c5e\u6027\uff1a\u7528\u6237ID\u3001\u5546\u54c1ID\u3001\u6570\u91cf\u3001\u603b\u91d1\u989d\n\u5173\u7cfb\uff1a\u8d2d\u7269\u8f66\u4e0e\u7528\u6237\u4e00\u5bf9\u4e00\u5173\u7cfb\u3001\u8d2d\u7269\u8f66\u4e0e\u5546\u54c1\u591a\u5bf9\u591a\u5173\u7cfb\n\n\u5bf9\u8c61\uff1a\u652f\u4ed8\n\u5c5e\u6027\uff1a\u652f\u4ed8ID\u3001\u8ba2\u5355ID\u3001\u652f\u4ed8\u65b9\u5f0f\u3001\u652f\u4ed8\u72b6\u6001\u3001\u652f\u4ed8\u65f6\u95f4\n\u5173\u7cfb\uff1a\u652f\u4ed8\u4e0e\u8ba2\u5355\u4e00\u5bf9\u4e00\u5173\u7cfb \n\n\u5bf9\u8c61\uff1a\u5546\u54c1\u641c\u7d22\n\u5c5e\u6027\uff1a\u641c\u7d22ID\u3001\u7528\u6237ID\u3001\u641c\u7d22\u5173\u952e\u8bcd\u3001\u641c\u7d22\u65f6\u95f4\n\u5173\u7cfb\uff1a\u5546\u54c1\u641c\u7d22\u4e0e\u7528\u6237\u4e00\u5bf9\u591a\u5173\u7cfb",
            "tool": "",
            "tool_labels": {},
            "tool_input": "",
            "created_at": 1737937195,
            "observation": "",
            "files": []
          }
        ],
        "message_files": [],
        "metadata": {},
        "status": "normal",
        "error": null,
        "parent_message_id": null
      }
    ]
  }
}

export const generateRuleCode = (body: Record<string, any>) => {
  return post<CodeGenRes>('/rule-code-generate', {
    body,
  })
}

export const fetchModelParams = (providerName: string, modelId: string) => {
  return get(`workspaces/current/model-providers/${providerName}/models/parameter-rules`, {
    params: {
      model: modelId,
    },
  }) as Promise<{ data: ModelParameterRule[] }>
}

export const fetchPromptTemplate = ({
  appMode,
  mode,
  modelName,
  hasSetDataSet,
}: { appMode: string; mode: ModelModeType; modelName: string; hasSetDataSet: boolean }) => {
  return get<Promise<{ chat_prompt_config: ChatPromptConfig; completion_prompt_config: CompletionPromptConfig; stop: [] }>>('/app/prompt-templates', {
    params: {
      app_mode: appMode,
      model_mode: mode,
      model_name: modelName,
      has_context: hasSetDataSet,
    },
  })
}

export const fetchTextGenerationMessage = ({
  appId,
  messageId,
}: { appId: string; messageId: string }) => {
  return get<Promise<any>>(`/apps/${appId}/messages/${messageId}`)
}
